package com.example.online_order.advice;




import com.example.online_order.Eception.BussinessException;
import com.example.online_order.Eception.SystemException;
import com.example.online_order.pojo.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

@RestControllerAdvice//wei当前项目的全部异常进行统一处理
public class projectExceptionAdvice {


    @ExceptionHandler(SystemException.class)
    public Result handleSystemException(SystemException se) {

        Result result = new Result();
        result.setMessage("系统异常：" + se.getMessage());
        //通知维护人员
        //写日志
        return result;
    }


    @ExceptionHandler(BussinessException.class)
    public Result handleBussinessException(BussinessException be) {
        Result result = new Result();
        switch (be.getCode()) {
            case bus_id_not_found:
                result.setMessage("业务异常：" + be.getMessage());
                break;
            case bus_integer_abc:
                result.setMessage("请你当个人");
                break;
            case bus_invalid_token:
                result.setStatus(444);
                result.setMessage("token无效重新登录");
                break;
            case bus_timeout_token:
                result.setStatus(444);
                result.setMessage("token过期请重新登录");
                break;
            case bus_invalid_password:
                result.setStatus(555);
                result.setMessage("密码错误");
                break;
            case bus_insert_fail:
                result.setStatus(555);
                result.setMessage("添加管理员失败");
                break;
            case bus_orderNum_not_found:
                result.setStatus(440);
                result.setMessage("订单号错误");
                break;
            case bus_invalid_phone:
                result.setStatus(555);
                result.setMessage("手机号错误");
                break;
            default:
                System.out.println(be.getCode());
                System.out.println(be.getMessage());
                result.setMessage("其他业务异常");
                break;
        }

        return result;
    }


    @ExceptionHandler(Exception.class)//处理项目的全部异常类型
    public Result handleAllException(Exception e) {
        e.printStackTrace();
        System.out.println(e.getMessage());
        //返回统一的异常结果，避免客户看到异常
        if(e instanceof MethodArgumentTypeMismatchException)
        {
            Result result = Result.fail("请输入数字", 666);
            result.setData(e.getMessage());
            return result;
        }
        else{
            Result result = Result.fail("系统繁忙", 777);
            System.out.println(e.getMessage());
            result.setData(e.getMessage());
            return result;
        }


    }


}
